package com.zz.hhbl.interceptor

import android.text.TextUtils
import com.zz.hhbl.config.GlobalCache
import okhttp3.Interceptor


/**
 * Copyright © 2020 zanezhao All rights reserved.
 * Created on 2020/3/26 12:45 PM.
 * @version: V1.0
 */
object CommonParameterInterceptor {
    fun builder(): Interceptor {
        return Interceptor { chain ->
            val request = chain.request()
            val token = GlobalCache.getToken()
            val volcanoVoiceToken = GlobalCache.getAiConfig().token
            val headers = hashMapOf(
                "Custom-Agent" to "Android",
            ).apply {
                if (TextUtils.isEmpty(token).not()) {
                    put("Authorization", "Bearer $token")
                }
//                if (TextUtils.isEmpty(volcanoVoiceToken).not() &&
//                    (request.url.toUrl().path.contains("/business/volcanoVoiceChat/startVoiceChat")||
//                    request.url.toUrl().path.contains("/business/volcanoVoiceChat/stopVoiceChat"))){
//                    put("Authorization","Bearer $volcanoVoiceToken")
//                }
            }

            val newRequest = request.newBuilder().apply {
                headers.forEach { (key, value) -> addHeader(key, value) }
            }.method(request.method, request.body).build()
            chain.proceed(newRequest)
        }
    }
}